package com.gzs.learn.admin.config;

import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.gzs.learn.common.util.SpringContextUtil;
import com.gzs.learn.logging.LogInterceptor;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.format.DateTimeFormatter;
import java.util.TimeZone;

import static com.gzs.learn.inf.GlobalConstant.DATE_FORMAT;
import static com.gzs.learn.inf.GlobalConstant.DATE_TIME_FORMAT;

/**
 * 程序注解配置
 *
 * @author guanzhisong
 */
@Configuration
public class ApplicationConfig {

    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jacksonObjectMapperCustomization() {
        return builder -> {
            builder.timeZone(TimeZone.getDefault());
            builder.simpleDateFormat(DATE_TIME_FORMAT);
            builder.serializers(new LocalDateSerializer(DateTimeFormatter.ofPattern(DATE_FORMAT)));
            builder.serializers(new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DATE_TIME_FORMAT)));
            // long型数据序列化到客户端为String类型
            builder.serializerByType(Long.class, ToStringSerializer.instance);
        };
    }

    @Bean
    public SpringContextUtil springContextUtil() {
        return new SpringContextUtil();
    }


    @Bean
    public LogInterceptor logInterceptor() {
        return new LogInterceptor();
    }
//    @Bean
//    public ClientSelectorConfig clientSelectorConfig() {
//        return new ClientSelectorConfig();
//    }
}
